﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NetCard.Models;

namespace NetCard.Dao
{
    public class RouterDao
    {
        private NetCardEntities _entities = new NetCardEntities();

        public List<Router> GetRouters(long projectId)
        {
            try
            {
                List<Router> projectList = new List<Router>();
                projectList = (List<Router>)(from c in _entities.Router
                                             where c.projectId == projectId
                                              select c);
                return projectList;
            }
            catch
            {
                return null;
            }
        }

        public void InsertRouter(Router router)
        {
            try
            {
                _entities.AddToRouter(router);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

        public Router GetRouter(long id)
        {
            try
            {
                var router = (from c in _entities.Router
                             where c.id == id
                             select c).FirstOrDefault();
                return (Router)router;
            }
            catch
            {
                return null;
            }
        }

        public void UpdateRouter(Router originalRouter, Router routerToEdit)
        {
            try
            {
                _entities.ApplyPropertyChanges(originalRouter.EntityKey.EntitySetName, routerToEdit);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

        public void DeleteRouter(Router router)
        {
            try
            {
                _entities.DeleteObject(router);
                _entities.SaveChanges();
            }
            catch
            {
            }
        }

        public Router GetRouterByXY(decimal lat, decimal lng)
        {
            try
            {
                var router = (from item in _entities.Router
                              where item.x == lat
                              where item.y == lng
                              select item).FirstOrDefault();
                return (Router)router;
            }
            catch
            {
                return null;
            }
        }


    }
}